package org.projsquirrel;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;


public class PictureActivity extends Activity {
	private Gallery gallery;
	private ImageView imgView;
	private Integer[] Imgid = {
			R.drawable.pic1, R.drawable.pic2, R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, 
			R.drawable.pic6, R.drawable.pic7,R.drawable.pic8
	    };

	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
        setContentView(R.layout.picture);
        imgView = (ImageView)findViewById(R.id.ImageView01);
        imgView.setImageResource(Imgid[0]);
        gallery = (Gallery) findViewById(R.id.picgallery);
        gallery.setAdapter(new ImageAdapter(this));
        gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            	imgView.setImageResource(Imgid[position]);
            }
        });
        
        final Button button = (Button) findViewById(R.id.footer);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	Intent i = new Intent(Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            			startActivityForResult(i, 0);
            }
        });
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    MenuInflater inflater = getMenuInflater();
	    inflater.inflate(R.menu.picture_menu, menu);
	    return true;
	}
	
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	    case R.id.menupic:
	        showGallery();
	        return true;
	    case R.id.help:
	        showHelp();
	        return true;
	    default:
	        return super.onOptionsItemSelected(item);
	    }
	}
	
	private void showGallery() {
		/*Intent i = new Intent();
		i.setType("image/*");
		i.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(i,"Select Picture"), 1);*/
	}
	
	private void showHelp() {
		Intent intent = new Intent(PictureActivity.this,About.class);
		View view = PictureGroup.group.getLocalActivityManager()  
        .startActivity("show_aboutpic", intent  
        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))  
        .getDecorView();  
  
        // Again, replace the view  
		PictureGroup.group.replaceView(view);    
	}
    
	 @Override
	    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    	super.onActivityResult(requestCode, resultCode, data);
	    	switch(requestCode) { 
	        case 0:
	            if(resultCode == RESULT_OK){  
	                Uri selectedImage = data.getData();
	                String[] filePathColumn = {MediaStore.Images.Media.DATA};

	                Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
	                cursor.moveToFirst();

	                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
	                String filePath = cursor.getString(columnIndex);
	                cursor.close();


	                Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
	                Toast.makeText( getApplicationContext() , "Photo submitted", Toast.LENGTH_SHORT);
	            }
	        }

	        };
 }
